CREATE PROCEDURE [dbo].[SlotSave]
@Id						UNIQUEIDENTIFIER,
@ProviderId				UNIQUEIDENTIFIER,
@SpecialtyId			UNIQUEIDENTIFIER,
@ClinicTypeId			UNIQUEIDENTIFIER,
@StartDateTime			DATETIME,
@EndDateTime			DATETIME,
@UBRN					INT,
@Status					INT
AS

DECLARE	@RowCount AS INT
DECLARE @Error AS INT
DECLARE @ProviderClinicTypeId AS UNIQUEIDENTIFIER

SET @ProviderClinicTypeId = (Select Id from ProviderClinicType WHERE ProviderId = @ProviderId AND ClinicTypeId = @ClinicTypeId)

UPDATE	Slot
SET		ProviderClinicTypeId	= ISNULL(@ProviderClinicTypeId, ProviderClinicTypeId),
		StartDateTime			= ISNULL(@StartDateTime, StartDateTime),
		EndDateTime				= ISNULL(@EndDateTime, EndDateTime),
		UBRN					= @UBRN,
		Status					= ISNULL(@Status, Status)
WHERE	Id = @Id

SELECT @Error = @@ERROR, @RowCount = @@ROWCOUNT

IF @Error  <> 0
BEGIN
	RAISERROR('ERROR::SlotSave:Error in',16,1)
END

IF @RowCount  > 0
BEGIN
	RETURN @RowCount 
END

INSERT INTO Slot(Id, ProviderClinicTypeId, StartDateTime, EndDateTime, UBRN, Status)
VALUES(@Id, @ProviderClinicTypeId, @StartDateTime, @EndDateTime, @UBRN, @Status)

SELECT	@Error = @@ERROR, @RowCount = @@ROWCOUNT

IF @Error  <> 0
BEGIN
	RAISERROR('ERROR::SlotNew:Error in',16,1)
END

RETURN @RowCount